package com.lboyang.provider.registry.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.lboyang.common.enums.RpcErrorMessageEnum;
import com.lboyang.common.exception.RpcException;
import com.lboyang.provider.registry.ServiceRegistry;
import com.lboyang.provider.registry.nacos.util.NamingServiceUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * @Description
 *      nacos 注册中心
 * @Date 2021/9/12 20:19
 * @author: A.iguodala
 */
@Slf4j
public class NacosServiceRegistryImpl implements ServiceRegistry {

    /**
     * 注册nacos服务
     * @param rpcServiceName 对应的服务名
     * @param inetSocketAddress 服务地址
     */
    @Override
    public void registerService(String rpcServiceName, InetSocketAddress inetSocketAddress) {
        try {
            NamingServiceUtil.namingService.registerInstance(rpcServiceName,
                    inetSocketAddress.getHostName(),
                    inetSocketAddress.getPort());
        } catch (NacosException e) {
            log.error("service registry failed: {}", e);
            throw new RpcException(RpcErrorMessageEnum.SERVICE_REGISTRY_FAILED);
        }
    }
}
